from django.db import models

# 自关联模型: 外键指向当前模型本身
class Area(models.Model):
    """省市区"""
    name = models.CharField(max_length=20, verbose_name='名称')
    parent = models.ForeignKey('self', on_delete=models.SET_NULL, related_name='subs', null=True, blank=True, verbose_name='上级行政区划')
    # area_set改名 subs
    class Meta:
        db_table = 'tb_areas'
        verbose_name = '省市区'
        verbose_name_plural = '省市区'

    def __str__(self):
        return self.name



# Area.objects.filter(parent=None)
# Area.objects.filter(parent__isnull=True)
#
# gd.subs.all()
# sz.subs.all()
#
# sz.parent
# ba.parent.parent
# Area.objects.get(id=130000)



# class BookInfo: 一
#     # heroinfo_set
#     whh
# class HeroInfo: 多
#     hbook = models.ForeignKey(BookInfo, related_name='whh')


# book.heroinfo_set.all()
# HeroInfo.objects.filter(hbook__btitle='xxx')
#
# hero.hbook
# BookInfo.objects.filter(heroinfo__hname='xxx')[0]